package com.ymatou.promotion.api.testcase;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.CouponBatchBean;
import com.ymatou.promotion.api.parameter.CouponValueInfoBean;
import com.ymatou.promotion.api.parameter.MyCouponInfoBean;
import com.ymatou.promotion.api.parameter.UserCouponListBean;
import com.ymatou.promotion.api.service.UserCouponListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponScenarioWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取买家我的优惠券
 *
 * @author chenjiazhu
 *
 *         逻辑：
 * 
 *         -- ============================================= -- Author: zhujinfeng -- Create date:
 *         2015-02-26 -- Description: 取用户所有优惠券列表 -- =============================================
 *         CREATE PROCEDURE [dbo].[SP_CouponListByUser]
 * @BuyerUserId INT,
 * @CouponState INT AS BEGIN
 * 
 *              SET NOCOUNT ON;
 * 
 *              SELECT a.sCouponCode, a.iCouponType, dbo.FUNC_MargeCouponValue(a.iCouponSetting) AS
 *              CouponValue, c.iCouponUseType, a.dValidStart, a.dValidEnd, f.iCouponUsedCount,
 *              e.sUsePlatforms, f.dAddTime,e.iUserType,e.sActivityIds
 *              ,e.sLogisticsTypes,e.sProductBrands,e.sProductCategories,
 *              e.sSellerIds,e.sSpecificProducts,e.sUserLevels,b.sCouponName,B. iBatchCreateType,
 *              case when iCouponUsedCount>0 AND a.dValidStart<=GETDATE() AND a.dValidEnd>=GETDATE()
 *              then 1 when iCouponUsedCount>0 AND a.dValidStart>GETDATE() then 2 when
 *              iCouponUsedCount=0 then 3 when iCouponUsedCount>0 AND a.dValidStart<=a.dValidEnd AND
 *              a.dValidEnd<GETDATE() then 4 else 5 END AS iUseState INTO #temp FROM Ymt_Coupon a
 *              LEFT JOIN Ymt_CouponBatch b ON a.iBatchId = b.iBatchId LEFT JOIN Ymt_CouponSetting c
 *              ON a.iCouponSetting = c.iCouponSettingId LEFT JOIN Ymt_CouponScenario e ON
 *              c.iScenarioId = e.iCouponScenarioId inner JOIN Ymt_CouponPrivateUserBound f ON
 *              a.sCouponCode = f.sCouponCode WHERE f.iUserId = @BuyerUserId
 * 
 *              IF @CouponState=1 --可使用 SELECT *,row_number() over(partition by iUseState order by
 *              iBatchCreateType,dAddTime desc) as rounum FROM #temp WHERE iUseState=1 ELSE IF
 * @CouponState=2 --未生效 SELECT *,row_number() over(partition by iUseState order by
 *                iBatchCreateType,dAddTime desc) as rounum FROM #temp WHERE iUseState=2 ELSE IF
 * @CouponState=3 --已使用 SELECT *,row_number() over(partition by iUseState order by
 *                iBatchCreateType,dAddTime desc) as rounum FROM #temp WHERE iUseState=3 ELSE IF
 * @CouponState=4 --已过期 SELECT *,row_number() over(partition by iUseState order by
 *                iBatchCreateType,dAddTime desc) as rounum FROM #temp WHERE iUseState=4 ---ELSE IF
 * @CouponState=5 --已作废 ---SELECT * FROM #temp WHERE dValidEnd<dValidStart ORDER BY
 *                dAddTime DESC ELSE SELECT *,row_number() over(partition by iUseState order by
 *                iBatchCreateType,dAddTime desc) as rounum FROM #temp WHERE iUseState!=5 END
 */
@TestSuite
public class Ts_UserCouponList {
    private static UserCouponListBean usercouponlistBean;
    private static UserCouponListCall usercouponlistCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取买家我的优惠券");
    }

    @Before
    public void caseUp() {
        usercouponlistBean = new UserCouponListBean();
        usercouponlistCall = new UserCouponListCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(usercouponlistCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_UserCouponList_001() {
        Logger.start(true, "获取买家我的优惠券-0全部全场优惠券");
        try {

            setUserCouponList(0);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_UserCouponList_002() {
        Logger.start(true, "获取买家我的优惠券-1全部全场优惠券");
        try {

            setUserCouponList(1);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /*
     * iCouponUsedCount>0 AND a.dValidStart>GETDATE()
     */
    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_UserCouponList_003() {
        Logger.start(true, "获取买家我的优惠券-2未生效全场优惠券");
        try {
            CouponCallService couponCallService = new CouponCallService();
            CouponBatchBean couponBatchBean = couponCallService
                    .setDefaultCouponBatchBean();

            Date dNow = new Date(); // 当前时间
            Date dAfter = new Date();

            Calendar calendar1 = Calendar.getInstance(); // 得到日历
            calendar1.setTime(dNow);// 把当前时间赋给日历
            calendar1.add(Calendar.DAY_OF_MONTH, 1); // 设置为前一天
            dAfter = calendar1.getTime(); // 得到前一天的时间

            couponBatchBean.setValidStart(dAfter);

            String batchCode = couponCallService.createSellerCoupon(
                    couponBatchBean).getBatchCode();

            couponCallService.receiveCouponInfo(batchCode,
                    Integer.parseInt(EnvSetup.getData("SellerId")));

            setUserCouponList(2);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /*
     * iCouponUsedCount=0
     */
    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_UserCouponList_004() {
        Logger.start(true, "获取买家我的优惠券-3已使用全场优惠券");
        try {

            String couponCode = new CouponCallService()
                    .receiveCouponInfo(Integer.parseInt(EnvSetup
                            .getData("SellerId")));
            new YmtCouponPrivateUserBoundWapper().UpdateiCouponUsedCount(
                    couponCode, 0);

            setUserCouponList(3);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * iCouponUsedCount>0 AND a.dValidStart<=a.dValidEnd AND a.dValidEnd<GETDATE()
     */
    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_UserCouponList_005() {
        Logger.start(true, "获取买家我的优惠券-4已过期全场优惠券");
        try {
            String couponCode = new CouponCallService()
                    .receiveCouponInfo(Integer.parseInt(EnvSetup
                            .getData("SellerId")));

            Date dNow = new Date(); // 当前时间
            Date dBefore = new Date();
            Date dAfter = new Date();

            Calendar calendar1 = Calendar.getInstance(); // 得到日历
            calendar1.setTime(dNow);// 把当前时间赋给日历
            calendar1.add(Calendar.DAY_OF_MONTH, -2); // 设置为前二天
            dBefore = calendar1.getTime(); // 得到前一天的时间

            Calendar calendar2 = Calendar.getInstance(); // 得到日历
            calendar2.setTime(dNow);// 把当前时间赋给日历
            calendar2.add(Calendar.DAY_OF_MONTH, -1); // 设置为前一天
            dAfter = calendar2.getTime(); // 得到前一天的时间

            new YmtCouponWapper().setdValidStart(dBefore, couponCode);
            new YmtCouponWapper().setdValidEnd(dAfter, couponCode);

            setUserCouponList(4);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_UserCouponList_006() {
        Logger.start(true, "获取买家我的优惠券-5已作废全场优惠券-现变成返回所有状态优惠券");
        try {
            setUserCouponList(5);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            verifyReturn();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_007() {
        Logger.start(false, "验证BuyerUserId空时返回报错或空");
        try {
            setUserCouponList(1);
            usercouponlistBean.setBuyerUserId(null);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals("共找到 0 个优惠券",
                    usercouponlistCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_008() {
        Logger.start(false, "验证BuyerUserId不存在时返回报错或空");
        try {
            setUserCouponList(1);
            usercouponlistBean.setBuyerUserId(7895545);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals("共找到 0 个优惠券",
                    usercouponlistCall.getMessageText(), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_009() {
        Logger.start(false, "验证CouponState空时返回报错或空");
        try {

            setUserCouponList(1);
            usercouponlistBean.setCouponState(null);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_010() {
        Logger.start(false, "验证CouponState不存在时返回报错或空");
        try {

            setUserCouponList(1);
            usercouponlistBean.setCouponState(6);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_011() {
        Logger.start(false, "验证PageSize空时返回空");
        try {

            setUserCouponList(1);
            usercouponlistBean.setPageSize(null);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

            Logger.verifyEquals(null, usercouponlistCall.getUserCouponListData()
                    .getUserCouponList(),
                    "验证PageSize空时返回UserCouponList无数据");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_012() {
        Logger.start(false, "验证PageSize=0时返回空");
        try {

            setUserCouponList(1);
            usercouponlistBean.setPageSize(0);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

            Logger.verifyEquals(null, usercouponlistCall.getUserCouponListData()
                    .getUserCouponList(),
                    "验证PageSize空时返回UserCouponList无数据");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_013() {
        Logger.start(false, "验证CurPage空时返回空");
        try {
            setUserCouponList(1);
            usercouponlistBean.setCurPage(null);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_014() {
        Logger.start(false, "验证CurPage=0时返回空");
        try {
            setUserCouponList(1);
            usercouponlistBean.setCurPage(0);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_015() {
        Logger.start(false, "验证CurPage取不存在页时返回空");
        try {
            setUserCouponList(1);
            usercouponlistBean.setCurPage(1000);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    
    @Test
    @Category(P2.class)
    @TestCase
    public void Tc_UserCouponList_016() {
        Logger.start(false, "验证用户没有优惠券时返回空的list而不是null，修复app端不兼容问题：RA-2234");
        try {
        	  usercouponlistBean.setBuyerUserId(45678);
              usercouponlistBean.setCouponState(0);
              usercouponlistBean.setPageSize(10000);
              usercouponlistBean.setCurPage(0);
            usercouponlistBean.setCurPage(1000);

            usercouponlistCall.setData(usercouponlistBean);
            usercouponlistCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, usercouponlistCall.getCode(),
                    "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, usercouponlistCall.getBCode(),
                    "验证返回Bcode");
            Logger.verifyEquals(true, usercouponlistCall.getMessageText()
                    .contains("优惠券"), "验证返回MessageText");
            Logger.verifyEquals(0,
                    usercouponlistCall.getUserCouponListData().getUserCouponList().size(),"验证返回UserCouponList size 为0"); 
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 设置默认请求bean
     * 
     * @param couponState
     * @throws ClientProtocolException
     * @throws IOException
     */
    private void setUserCouponList(int couponState)
            throws ClientProtocolException, IOException {
        int buyerId = Integer.valueOf(EnvSetup.getData("SellerId"));
        usercouponlistBean.setBuyerUserId(buyerId);
        usercouponlistBean.setCouponState(couponState);
        usercouponlistBean.setPageSize(10000);
        usercouponlistBean.setCurPage(0);
    }

    private void verifyReturn() {
        Logger.verifyEquals("200", usercouponlistCall.getCode(), "验证返回code");
        Logger.verifyEquals("0", usercouponlistCall.getBCode(), "验证返回Bcode");
        Logger.verifyEquals("共找到 " + usercouponlistCall.getTotals() + " 个优惠券",
                usercouponlistCall.getMessageText(), "验证返回MessageText");

        List<MyCouponInfoBean> UserCouponList = usercouponlistCall
                .getUserCouponListData().getUserCouponList();
        Date now = new Date();

        if (UserCouponList != null && UserCouponList.size() > 0)
        {
            for (MyCouponInfoBean myCouponInfoBean : UserCouponList) {
                Logger.comment("验证CouponCode=" + myCouponInfoBean.getCouponCode() + "的记录");

                String couponCode = myCouponInfoBean.getCouponCode();
                YmtCoupon coupon = new YmtCouponWapper().selectBysCouponCode(
                        couponCode).get(0);
                YmtCouponPrivateUserBound couponPrivateUserBound = new YmtCouponPrivateUserBoundWapper()
                        .selectBysCouponCode(couponCode).get(0);
                YmtCouponBatch couponBatch = new YmtCouponBatchWapper()
                        .selectByPrimaryKey(coupon.getiBatchId());
                YmtCouponValue couponValue = new YmtCouponValueWapper()
                        .selectByiCouponSettingId(couponBatch.getiCouponSettingId())
                        .get(0);
                YmtCouponSetting couponSetting = new YmtCouponSettingWapper()
                        .selectByiCouponSettingId(couponBatch.getiCouponSettingId())
                        .get(0);
                YmtCouponScenario couponScenario = new YmtCouponScenarioWapper()
                        .selectByiCouponScenarioId(couponSetting.getiScenarioId())
                        .get(0);

                // CouponTitle
                String couponTitle = "订单满" + couponValue.getFminordervalue()
                        + "元, 抵扣" + couponValue.getFcouponvalue() + "元";
                Logger.verifyEquals(couponTitle, myCouponInfoBean.getCouponTitle()
                        .get(0), "验证返回CouponTitle与数据库一致");
                CouponValueInfoBean value = myCouponInfoBean.getCouponValues().get(
                        0);
                // for (CouponValueInfoBean couponValueInfoBean : list) {
                // Logger.debug("couponValueInfoBean:"+FormatUtil.beanToGSONString(couponValueInfoBean));
                // }
                // CouponValues
                Logger.verifyEquals(
                        0,
                        value.getCouponValue().compareTo(
                                couponValue.getFcouponvalue()),
                        "验证返回CouponValue与数据库表Ymt_CouponValue中字段CouponValue一致");
                Logger.verifyEquals(
                        0,
                        value.getOrderValue().compareTo(
                                couponValue.getFminordervalue()),
                        "验证返回OrderValue与数据库表Ymt_CouponValue中字段Fminordervalue一致");
                Logger.verifyEquals(couponSetting.getiCouponUseType(),
                        value.getCouponUseType(),
                        "验证返回CouponUseType与数据库表Ymt_CouponValue中字段CouponUseType一致");

                // 验证Platform
                if (myCouponInfoBean.getPlatform().size() > 0) {
                    Logger.verifyEquals(true, isPlatformsContain(couponScenario.getSuseplatforms(),
                            myCouponInfoBean.getPlatform().get(0)),
                            "验证返回Platform与数据库表YmtCouponScenario中字段Suseplatforms一致");
                } else {
                    Logger.verifyIsNull(couponScenario.getSuseplatforms(),
                            "验证返回Platform与数据库表YmtCouponScenario中字段Suseplatforms一致");

                }

                // 验证ValidStart，ValidEnd
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Logger.verifyEquals(formatter.format(coupon.getdValidStart()),
                        myCouponInfoBean.getValidBegin(),
                        "验证返回ValidBegin与数据库表Ymt_Coupon中字段dValidStart一致");
                Logger.verifyEquals(formatter.format(coupon.getdValidEnd()),
                        myCouponInfoBean.getValidEnd(),
                        "验证返回ValidEnd与数据库表Ymt_Coupon中字段dValidEnd一致");

                // AllowUseCount
                Logger.verifyEquals(couponPrivateUserBound.getiCouponUsedCount(),
                        myCouponInfoBean.getAllowUseCount(),
                        "验证返回AllowUseCount与数据库表ymt_couponPrivateUserBound字段iCouponUsedCount一致");

                // UseType
                Logger.verifyEquals(couponSetting.getiCouponUseType(),
                        myCouponInfoBean.getUseType(),
                        "验证返回UseType与数据库表ymt_couponSetting中字段iCouponUseType一致");

                // UseState 0，5返回所有状态的优惠券
                if (usercouponlistBean.getCouponState() != 0 && usercouponlistBean.getCouponState() != 5) {
                    Logger.verifyEquals(usercouponlistBean.getCouponState(), myCouponInfoBean.getUseState(),
                            "验证返回UseState正确");
                }
                // ReceiveDate
                Logger.verifyEquals(
                        getDateString(couponPrivateUserBound.getdAddTime()),
                        getDateString(myCouponInfoBean.getReceiveDate()),
                        "验证返回ReceiveDate与数据库表ymt_couponPrivateUserBound中dAddTime一致");

                // Scenario
                String scenario = EnvSetup.getData("SellerId") + "|"
                        + EnvSetup.getData("SellerName");

                if (myCouponInfoBean.getScenario() != null && myCouponInfoBean.getScenario().size() > 0)
                {
                    Logger.verifyEquals(scenario,
                            myCouponInfoBean.getScenario().get(0), "验证返回Scenario与正确性");
                }

                // UserType
                Logger.verifyEquals(couponScenario.getIusertype(),
                        myCouponInfoBean.getUserType(),
                        "验证返回UserType与数据库表ymt_couponScenario字段Iusertype一致");

                // ScenarioType
                Logger.verifyEquals(getScenarioType(couponScenario), myCouponInfoBean.getScenarioType(),
                        "验证返回ScenarioType");

                // Activity

                // Logistics

                // CouponName
                Logger.verifyEquals(couponBatch.getsCouponName(),
                        myCouponInfoBean.getCouponName(), "验证返回CouponName与数据库一致");

                // CreateType
                Logger.verifyEquals(Integer.parseInt(couponBatch
                        .getiBatchCreateType().toString()), myCouponInfoBean
                        .getCreateType(),
                        "验证返回CreateType与数据库中表Ymt_couponBatch中字段iBatchCreateType一致");

                // 验证所有
                // iCouponUsedCount>0 AND a.dValidStart<=GETDATE() AND
                // a.dValidEnd>=GETDATE()
                if (usercouponlistBean.getCouponState() == 1) {
                    Logger.verifyEquals(true,
                            couponPrivateUserBound.getiCouponUsedCount() > 0,
                            "验证数据库ymt_couponPrivateUserBound中iCouponUsedCount>0");
                    Logger.verifyEquals(true, coupon.getdValidStart().before(now),
                            "验证数据库ymt_coupon中dValidStart<=GETDATE(),dValidStart为" + coupon.getdValidStart().toString());
                    Logger.verifyEquals(true, coupon.getdValidEnd().after(now),
                            "验证数据库ymt_coupon中dValidEnd>=GETDATE(),dValidEnd为" + coupon.getdValidEnd().toString());
                }

                // 未生效
                // 逻辑：iCouponUsedCount>0 AND a.dValidStart>GETDATE()
                if (usercouponlistBean.getCouponState() == 2) {
                    Logger.verifyEquals(true,
                            couponPrivateUserBound.getiCouponUsedCount() > 0,
                            "验证数据库ymt_couponPrivateUserBound中iCouponUsedCount>0");
                    Logger.verifyEquals(true, coupon.getdValidStart().after(now),
                            "验证数据库ymt_coupon中dValidStart>GETDATE()");
                }

                // 已使用
                // iCouponUsedCount=0
                if (usercouponlistBean.getCouponState() == 3) {
                    Logger.verifyEquals(true,
                            couponPrivateUserBound.getiCouponUsedCount() == 0,
                            "验证数据库ymt_couponPrivateUserBound中iCouponUsedCount>0");
                }

                // 已过期
                // iCouponUsedCount>0 AND a.dValidStart<=a.dValidEnd AND
                // a.dValidEnd<GETDATE()
                if (usercouponlistBean.getCouponState() == 4) {
                    Logger.verifyEquals(true,
                            couponPrivateUserBound.getiCouponUsedCount() > 0,
                            "验证数据库ymt_couponPrivateUserBound中iCouponUsedCount>0");
                    Logger.verifyEquals(true,
                            coupon.getdValidStart().before(coupon.getdValidEnd()),
                            "验证数据库ymt_coupon中dValidStart<=dValidEnd");
                    Logger.verifyEquals(true, coupon.getdValidEnd().before(now),
                            "验证数据库ymt_coupon中dValidEnd<GETDATE()");
                }

                // 已作废
                if (usercouponlistBean.getCouponState() == 5) {
                    Logger.verifyEquals(false, couponBatch.getbInvalidStatus(),
                            "验证数据库中表ymt_couponBatch字段bInvalidStatus正确");
                }
            }
        }
        else {
            Logger.fail(new Exception("没有返回数据！"));
        }
    }

    private String getDateString(Date date) {
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");

        return formatter1.format(date) + "T" + formatter2.format(date);
    }

    // 2016-04-11T16:41:45.12转换
    private String getDateString(String date) {
        return date.substring(0, 19);
    }

    private boolean isPlatformsContain(String exceptStr, String actualStr)
    {
        String platforms[] = exceptStr.split(",");

        String acArray[] = actualStr.split("|");

        String ac = acArray[0];

        for (String str : acArray) {
            if (str.equals(ac))
                return true;
        }

        return false;
    }

    private int getScenarioType(YmtCouponScenario scenario)
    {
        int iScenarioType = 0;
        if (!IsNullOrWhiteSpace(scenario.getSproductbrands()))
        {
            iScenarioType = 1;
        }

        if (!IsNullOrWhiteSpace(scenario.getSproductcategories()))
        {
            iScenarioType = 2;
        }

        if (!IsNullOrWhiteSpace(scenario.getSspecificproducts()))
        {
            iScenarioType = 3;
        }

        if (!IsNullOrWhiteSpace(scenario.getSsellerids()))
        {
            iScenarioType = 4;
        }

        return iScenarioType;
    }

    private boolean IsNullOrWhiteSpace(String str)
    {
        return str == null || str.equals("");
    }
}
